package com.java.rabbitmq.topic;

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author Woo_home
 * @create 2020/5/27 13:07
 */

public class TopicProducer {

    // 队列名称
    public static final String EXCHANGE_NAME = "topics_exchange";

    public static void main(String[] args) throws Exception {

        // 创建连接
        Connection connection = ConnectionUtils.getConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 声明交换机以及类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        String[] routing_keys = new String[]{"abc.news", "abc.weather", "efg.news", "efg.weather"};
        String[] messages = new String[]{"abc新闻", "abc天气", "efg新闻", "efg天气"};

        for (int i = 0; i < routing_keys.length; i++) {
            String routingKey = routing_keys[i];
            String message = messages[i];
            channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
            System.out.printf("发送到路由的消息是 ：%s，内容是：%s%n ", routingKey, message);
        }

        // 关闭通道
        channel.close();
        connection.close();
    }
}
